package com.slanmao.controller;

import com.slanmao.common.entity.InputMessage;
import com.slanmao.common.entity.Response;
import com.slanmao.common.entity.UserInfo;
import com.slanmao.common.utils.TokenUtils;
import com.slanmao.entity.AreaServ;
import com.slanmao.entity.LocationVo;
import com.slanmao.exception.CommonException;
import com.slanmao.service.AreaServService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.slanmao.common.utils.ExcelUtils.excelToAreaServ;

/**
 * <CODE>AreaServiceController</CODE>
 * 区域服务控制器
 * @author fenrir
 * @Date 18-11-5
 */

@Controller
@Slf4j
public class AreaServiceController {



    @Autowired
    AreaServService areaServService;

    @Autowired
    com.slanmao.service.MsgService msgService;

    @Autowired
    TokenUtils tokenUtils;



    @GetMapping("/search")
    @ResponseBody
    public Response<String> searchByKeyword(@RequestParam("keyword") String keyword) {
        log.info("查询字段为" + keyword);
        AreaServ areaServ = areaServService.findByKeyWord(keyword);
        return new Response<>(areaServ.toString());
    }

    @PostMapping("/upload")
    @ResponseBody
    public Response<Boolean> uploadData(@RequestParam("file") MultipartFile file) throws IOException {
        List<AreaServ> list = excelToAreaServ(file);
        areaServService.uploadBatch(list);
        return new Response<>(true);
    }


    @GetMapping("/listLocation")
    public @ResponseBody
    Response checkValidate(@RequestParam("page")int start, @RequestParam("limit")int size) throws CommonException {
        List<LocationVo> result = new ArrayList<>();


        List<InputMessage> list = msgService.listLocationMsg(start > 0 ? start - 1 : 0, size);
        for (InputMessage inputMessage : list) {
            LocationVo vo = new LocationVo();
            UserInfo userInfo = tokenUtils.getUserInfo(inputMessage.getFromUserName());

            vo.setNickname(userInfo.getNickname());
            vo.setHeadimg(userInfo.getHeadimgurl());
            vo.setSubscribe_time(new Date(userInfo.getSubscribe_time()*1000));
            vo.setSubscribe_scene(userInfo.getSubscribe_scene());
            vo.setSex(userInfo.getSex());
            vo.setFormat_location(inputMessage.getFormat_location());
            vo.setLatitude(inputMessage.getLatitude());
            vo.setLongitude(inputMessage.getLongitude());
            vo.setCreatetime(new Date(inputMessage.getCreateTime()*1000));
            result.add(vo);
        }
        Integer count = msgService.locationMsgCount();
        Response response = new Response();
        response.setCode(200);
        response.setTotal(count);
        response.setData(result);
        return response;
    }



}
